package com.xuecheng.content.service.impl;

import com.xuecheng.base.exception.StudySystemException;
import com.xuecheng.content.config.MultipartSupportConfig;
import com.xuecheng.content.feignclient.MediaServiceFeignClient;
import com.xuecheng.content.model.dto.CoursePreviewDto;
import com.xuecheng.content.service.BuilderHtmlPageService;
import com.xuecheng.content.service.CoursePublishService;
import com.xuecheng.media.model.dto.UploadFileResultDto;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import lombok.Cleanup;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Objects;

/**
 * @author Tang
 * @version 1.0
 * @date 2024/1/9 15:15
 */
@Slf4j
@Service
public class BuilderHtmlPageServiceImpl implements BuilderHtmlPageService {
    private final String STRING_UTF_8 = "UTF-8";
    private final CoursePublishService coursePublishService;
    private final MediaServiceFeignClient mediaServiceFeignClient;
    public BuilderHtmlPageServiceImpl(CoursePublishService coursePublishService, MediaServiceFeignClient mediaServiceFeignClient) {
        this.coursePublishService = coursePublishService;
        this.mediaServiceFeignClient = mediaServiceFeignClient;
    }

    @Override
    public void uploadHtmlPage(File htmlFile, String uploadFilePath, String uploadFileName) throws IOException {
        MultipartFile multipartFile = MultipartSupportConfig.getMultipartFile(htmlFile);
        UploadFileResultDto uploadFileResultDto = mediaServiceFeignClient.uploadCoursePic(multipartFile, uploadFilePath
                .concat(File.separator)
                .concat(uploadFileName));
        if (Objects.isNull(uploadFileResultDto)) {
            StudySystemException.cast("上传文件发生熔断降级");
        }
    }

    @Override
    public File builderHtmlPageFile(String templatePath,
                                    String templateFileName,
                                    String builderFilePath,
                                    String builderFileName,
                                    Long courseId) throws IOException, TemplateException {
        File builderPath = new File(builderFilePath);
        if (!builderPath.exists()) {
            builderPath.mkdir();
        }
        // 生成html页面
        Configuration configuration = new Configuration(Configuration.getVersion());
        String path = Objects.requireNonNull(this.getClass().getResource(File.separator)).getPath();
        configuration.setDirectoryForTemplateLoading(new File(path.concat(templatePath)));
        configuration.setDefaultEncoding(STRING_UTF_8);
        Template template = configuration.getTemplate(templateFileName);

        // 获取模版数据
        CoursePreviewDto coursePreviewInfo = coursePublishService.getCoursePreviewInfo(courseId);
        coursePreviewInfo.setToVideo("#");
        String html = FreeMarkerTemplateUtils.processTemplateIntoString(template, new HashMap<String, Object>() {{
            put("model", coursePreviewInfo);
        }});

        @Cleanup InputStream is = IOUtils.toInputStream(html, STRING_UTF_8);
        File htmlFile = new File(builderFilePath.concat(builderFileName));
        @Cleanup FileOutputStream os = new FileOutputStream(htmlFile);
        IOUtils.copy(is, os);
        return htmlFile;
    }
}
